package net.simflow.swf.analyzer.network.opt;

import java.util.HashMap;
import java.util.HashSet;

import net.simflow.swf.analyzer.network.data.ProcessorConfig;

import uk.org.taverna.scufl2.api.activity.Activity;
import uk.org.taverna.scufl2.api.configurations.Configuration;
import uk.org.taverna.scufl2.api.container.WorkflowBundle;
import uk.org.taverna.scufl2.api.core.Processor;
import uk.org.taverna.scufl2.api.core.Workflow;
import uk.org.taverna.scufl2.api.profiles.ProcessorBinding;
import uk.org.taverna.scufl2.api.profiles.Profile;

public class ProcessorMapExtracter {

	private static ProcessorMapExtracter instance = new ProcessorMapExtracter();

	private ProcessorMapExtracter() {
	}

	public static ProcessorMapExtracter getInstance() {
		return instance;
	}

	public HashMap<Processor, ProcessorConfig> extract(
			WorkflowBundle workflowBundle) {
		HashMap<Processor, ProcessorConfig> map = new HashMap<Processor, ProcessorConfig>();

		for (Profile pf : workflowBundle.getProfiles()) {
			for (ProcessorBinding pb : pf.getProcessorBindings()) {
				Processor p = pb.getBoundProcessor();
				Activity a = pb.getBoundActivity();
				Configuration c = pf.getConfigurations().getByName(a.getName());
				map.put(p, new ProcessorConfig(p, a, c));
			}
		}

		HashSet<Processor> existSet = new HashSet<Processor>();
		for (Workflow fl : workflowBundle.getWorkflows()) {
			existSet.addAll(fl.getProcessors());
		}
		existSet.removeAll(map.keySet());
		for (Processor p : existSet) {
			map.put(p, null);
		}

		return map;
	}
}
